package com.app.twth.ui.task;

import android.widget.RadioGroup;

import androidx.lifecycle.ViewModelProvider;
import androidx.viewbinding.ViewBinding;

import com.app.twth.R;
import com.app.twth.base.BaseFragment;
import com.app.twth.databinding.FragmentTaskBinding;
import com.app.twth.utils.CommonUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * ****************************************************************
 * 文件名称: FollowersFragment
 * 文件描述: 使用 Room 持久化存储列表数据
 * 注意事项:
 * 版权声明:
 * ****************************************************************
 */
public class TaskFragment extends BaseFragment {
    private int index = 0;
    private FragmentTaskBinding binding;
    private TaskViewModel viewModel;
    protected boolean hasRequested;//在onResume中判断是否已经请求过数据。用于懒加载


    @Override
    protected ViewBinding getViewBinding() {
        binding = FragmentTaskBinding.inflate(getLayoutInflater());
        return binding;
    }

    @Override
    protected void initListener() {
        binding.refreshLayout.setOnRefreshListener(v -> viewModel.getFollowers(true));
        binding.refreshLayout.setOnLoadmoreListener(v -> viewModel.getFollowers(false));
    }

    @Override
    protected void initData() {
        viewModel = new ViewModelProvider(requireActivity(), new ViewModelProvider.NewInstanceFactory()).get(TaskViewModel.class);
        binding.setViewModel(viewModel);
        binding.rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_pending:
                        index = 0;
                        break;
                    case R.id.rb_done:
                        index = 1;
                        break;
                }
                hasRequested = true;
                binding.refreshLayout.autoRefresh();//触发自动刷新

            }
        });
    }

    @Override
    protected void initViewObservable() {
        //根据请求调整UI
        viewModel.followersResultCode.observe(this, code -> {
            if (code.equals("0")) {
                //关闭下拉
                binding.refreshLayout.finishRefresh();
                binding.refreshLayout.finishLoadmore();
            } else {
                CommonUtil.toast(viewModel.followersResultMsg);
                //关闭下拉
                binding.refreshLayout.finishRefresh();
                binding.refreshLayout.finishLoadmore();
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        if (!hasRequested) {
            hasRequested = true;
            binding.refreshLayout.autoRefresh();//触发自动刷新
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMainThread(Object object) {
    }

    @Override
    public void onDestroy() {
        if (viewModel != null) {
            viewModel.dispose();
            viewModel = null;
        }
        super.onDestroy();
    }
}